package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.entity.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.ProductDao;
import io.renren.modules.sys.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String isTackOut = (String)params.get("takeout");
        String name = (String)params.get("name");
        String categoryid = (String)params.get("categoryid");

        IPage<ProductEntity> page = this.page(
                    new Query<ProductEntity>().getPage(params),
                    new QueryWrapper<ProductEntity>().like(StringUtils.isNotBlank(name),"productName",name)
                            .like(StringUtils.isNotBlank(name),"productName",name)
                            .eq(StringUtils.isNotBlank(isTackOut),"isTackOut",Integer.parseInt(isTackOut))
                            .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));
        return new PageUtils(page);
    }

    @Override
    public PageUtils getProductList(Map<String, Object> params){

        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        Integer isTackOut = null;
        if( params.get("TackOut")!=null&&!params.get("TackOut").equals("")){
            isTackOut = Integer.parseInt(params.get("TackOut").toString());
        }

        String productname = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){
            productname = params.get("name").toString();
        }

        String categoryids = null;
        if( params.get("categoryid")!=null&&!params.get("categoryid").equals("")){
            categoryids = params.get("categoryid").toString();
        }

        int count = baseMapper.queryListcount(isTackOut,productname,categoryids);
        List<ProductEntity> list = baseMapper.getProductList(isTackOut,productname,categoryids,(currPage-1)*pageSize,currPage*pageSize);

        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public PageUtils getLowerProductList(Map<String, Object> params){
        String name = (String)params.get("name");
        String categoryid = (String)params.get("categoryid");
        String shelvesids = (String)params.get("shelvesid");

        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数

        String productname = null;
        if( params.get("name")!=null&&!params.get("name").equals("")){
            productname = params.get("name").toString();
        }

        String categoryids = null;
        if( params.get("categoryid")!=null&&!params.get("categoryid").equals("")){
            categoryids = params.get("categoryid").toString();
        }

        Integer shelvesid = Integer.parseInt(shelvesids);
        int count = baseMapper.queryLowerListcount(productname,categoryids,shelvesid);
        List<ProductEntity> list = baseMapper.getLowerProductList(productname,categoryids,shelvesid,(currPage-1)*pageSize,currPage*pageSize);

        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public List<ProductAnalysisEntity> getProductAnalysis(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Object shelves = params.get("shelvesid");
        Object productname = params.get("productname");
        Integer shelvesid = 0;
        String pnstring = null;
        if(shelves !=null && !shelves.equals("")){
            shelvesid = Integer.parseInt(shelves.toString());
        }

        if(productname !=null && !productname.equals("")){
            pnstring = productname.toString();
        }

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        List<ProductAnalysisEntity> productAnalysisEntityList = baseMapper.getProductAnalysis(shelvesid,pnstring,startime,endtime);
        return productAnalysisEntityList;
    }

    @Override
    public ProductAnalysisStatisticsEntity getStatistics(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Object shelves = params.get("shelvesid");
        Object productname = params.get("productname");
        Integer shelvesid = 0;
        String pnstring = null;
        if(shelves !=null && !shelves.equals("")){
            shelvesid = Integer.parseInt(shelves.toString());
        }

        if(productname !=null && !productname.equals("")){
            pnstring = productname.toString();
        }

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        ProductAnalysisStatisticsEntity entity = baseMapper.getStatistics(shelvesid,pnstring,startime,endtime);
        return entity;
    }

    @Override
    public ProductVOEntity getLowerProductInfo(Map<String, Object> params){
        String productids = (String)params.get("productid");
        String shelvesids = (String)params.get("shelvesid");


        Integer productid = Integer.parseInt(productids);

        Integer shelvesid = Integer.parseInt(shelvesids);

        ProductVOEntity productVOEntity = baseMapper.getLowerProductInfo(shelvesid,productid);
        return productVOEntity;
    }

}
